Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
Loading...
Searching...
No Matches
units.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015 Roc authors
3 *
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7 */
8
9//! @file roc_packet/units.h
10//! @brief Various units used in packets.
11
12#ifndef ROC_PACKET_UNITS_H_
13#define ROC_PACKET_UNITS_H_
14
15#include "roc_core/stddefs.h"
16#include "roc_core/time.h"
17
18namespace roc {
19namespace packet {
20
21//! Packet source ID identifying packet stream.
22typedef uint32_t source_t;
23
24//! Packet sequence number in packet stream.
25typedef uint16_t seqnum_t;
26
27//! Packet sequence numbers difference.
28typedef int16_t seqnum_diff_t;
29
30//! Compute difference between two seqnums.
32 return seqnum_diff_t(a - b);
33}
34
35//! Check if a is before b taking possible wrap into account.
36inline bool seqnum_lt(seqnum_t a, seqnum_t b) {
37 return seqnum_diff(a, b) < 0;
38}
39
40//! Check if a is before or equal to b taking possible wrap into account.
41inline bool seqnum_le(seqnum_t a, seqnum_t b) {
42 return seqnum_diff(a, b) <= 0;
43}
44
45//! Audio packet timestamp.
46typedef uint32_t timestamp_t;
47
48//! Audio packet timestamps difference.
49typedef int32_t timestamp_diff_t;
50
51//! Compute difference between two timestamps.
53 return timestamp_diff_t(a - b);
54}
55
56//! Check if a is before b taking possible wrap into account.
58 return timestamp_diff(a, b) < 0;
59}
60
61//! Check if a is before or equal to b taking possible wrap into account.
63 return timestamp_diff(a, b) <= 0;
64}
65
66//! Convert nanoseconds to number of samples.
68 return timestamp_diff_t(roundf(float(ns) / core::Second * sample_rate));
69}
70
71//! Convert number of samples to nanoseconds.
73 return core::nanoseconds_t(roundf(float(ts) / sample_rate * core::Second));
74}
75
76//! Bitmask of channels present in audio packet.
77typedef uint32_t channel_mask_t;
78
79//! Get number of channels in mask.
80static inline size_t num_channels(channel_mask_t ch_mask) {
81 size_t n_ch = 0;
82 for (; ch_mask != 0; ch_mask >>= 1) {
83 if (ch_mask & 1) {
84 n_ch++;
85 }
86 }
87 return n_ch;
88}
89
90//! FEC block number in a packet stream.
91typedef uint16_t blknum_t;
92
93//! FEC block numbers difference.
94typedef int16_t blknum_diff_t;
95
96//! Compute difference between two FEC block numbers.
98 return blknum_diff_t(a - b);
99}
100
101//! Check if a is before b taking possible wrap into account.
102inline bool blknum_lt(blknum_t a, blknum_t b) {
103 return blknum_diff(a, b) < 0;
104}
105
106//! Check if a is before or equal to b taking possible wrap into account.
107inline bool blknum_le(blknum_t a, blknum_t b) {
108 return blknum_diff(a, b) <= 0;
109}
110
111} // namespace packet
112} // namespace roc
113
114#endif // ROC_PACKET_UNITS_H_
const nanoseconds_t Second
One second represented in nanoseconds.
Definition: time.h:33
int64_t nanoseconds_t
Nanoseconds.
Definition: time.h:21
core::nanoseconds_t timestamp_to_ns(timestamp_diff_t ts, size_t sample_rate)
Convert number of samples to nanoseconds.
Definition: units.h:72
blknum_diff_t blknum_diff(blknum_t a, blknum_t b)
Compute difference between two FEC block numbers.
Definition: units.h:97
uint32_t source_t
Packet source ID identifying packet stream.
Definition: units.h:22
int16_t seqnum_diff_t
Packet sequence numbers difference.
Definition: units.h:28
bool seqnum_le(seqnum_t a, seqnum_t b)
Check if a is before or equal to b taking possible wrap into account.
Definition: units.h:41
timestamp_diff_t timestamp_from_ns(core::nanoseconds_t ns, size_t sample_rate)
Convert nanoseconds to number of samples.
Definition: units.h:67
seqnum_diff_t seqnum_diff(seqnum_t a, seqnum_t b)
Compute difference between two seqnums.
Definition: units.h:31
int16_t blknum_diff_t
FEC block numbers difference.
Definition: units.h:94
bool timestamp_lt(timestamp_t a, timestamp_t b)
Check if a is before b taking possible wrap into account.
Definition: units.h:57
bool blknum_le(blknum_t a, blknum_t b)
Check if a is before or equal to b taking possible wrap into account.
Definition: units.h:107
uint16_t seqnum_t
Packet sequence number in packet stream.
Definition: units.h:25
uint32_t timestamp_t
Audio packet timestamp.
Definition: units.h:46
bool seqnum_lt(seqnum_t a, seqnum_t b)
Check if a is before b taking possible wrap into account.
Definition: units.h:36
uint32_t channel_mask_t
Bitmask of channels present in audio packet.
Definition: units.h:77
uint16_t blknum_t
FEC block number in a packet stream.
Definition: units.h:91
bool blknum_lt(blknum_t a, blknum_t b)
Check if a is before b taking possible wrap into account.
Definition: units.h:102
timestamp_diff_t timestamp_diff(timestamp_t a, timestamp_t b)
Compute difference between two timestamps.
Definition: units.h:52
int32_t timestamp_diff_t
Audio packet timestamps difference.
Definition: units.h:49
bool timestamp_le(timestamp_t a, timestamp_t b)
Check if a is before or equal to b taking possible wrap into account.
Definition: units.h:62
Root namespace.
Commonly used types and functions.
Time definitions.